package com.schedule.task.controller;

import com.schedule.task.dao.BookBillMapper;
import com.schedule.task.entity.BookBill;
import com.schedule.task.service.BookBillService;
import com.schedule.task.vo.BillToUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
public class TestsController {

    private final BookBillMapper bookBillMapper;

    private final BookBillService bookBillService;


    @Autowired
    public TestsController(BookBillMapper bookBillMapper, BookBillService bookBillService) {
        this.bookBillMapper = bookBillMapper;
        this.bookBillService = bookBillService;
    }


    @GetMapping("/test/good")
    public BookBill testInsert()
    {
       Integer current = (int) (System.currentTimeMillis() /1000);
       BookBill bookBill = new BookBill();
       bookBill.setUid(1);
       bookBill.setDirect( (byte) 1);
       bookBill.setCreateAt(current);
       bookBill.setTotal(new BigDecimal("10"));
       bookBill.setState((byte) 1);
       this.bookBillMapper.insertSelective(bookBill);

       return  bookBill;
    }

    @GetMapping("/find")
    public BookBill findOne(@RequestParam("id") Integer id)
    {
        BookBill bookBill = bookBillService.findById(id);
        return  bookBill;
    }

    @GetMapping("/delete")
    public Integer delete(@RequestParam("id") Integer id)
    {
        try {
            bookBillService.deleteId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  id;
    }

    @GetMapping("/update")
    public Integer update(@RequestParam("id") Integer id)
    {
        BookBill bookBill = new BookBill();
        bookBill.setUid(100);
        bookBill.setDirectText("白鸽");
        bookBill.setId(id);
        try {
            bookBillService.updateId(bookBill);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  id;
    }

    @GetMapping("/insert")
    public Integer insert()
    {
        BookBill bookBill = new BookBill();
        Integer current = (int) (System.currentTimeMillis() /1000);
        bookBill.setUid(1);
        bookBill.setDirect( (byte) 1);
        bookBill.setCreateAt(current);
        bookBill.setTotal(new BigDecimal("10"));
        bookBill.setState((byte) 1);
        try {
            bookBillService.insertOne(bookBill);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    @GetMapping("/bill-user")
    public void billWithUser(@RequestParam("id") Integer id)
    {
        List<BillToUserVo> billToUserVos = this.bookBillService.getBillWithUser(id);
        System.out.println(billToUserVos);

    }


}
